<template>
    <div>
        <div class="filter">
            <el-form :inline="true" :model="formInline" ref="ruleForm">
                <el-form-item label="小区名称" prop="name">
                    <el-input v-model="formInline.name" placeholder="请输入小区名称"></el-input>
                </el-form-item>
                <el-form-item label="小区位置" prop="place">
                    <el-input v-model="formInline.place" placeholder="请输入小区位置"></el-input>
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="onSubmit">查询</el-button>
                    <el-button @click="resetForm('ruleForm')">重置</el-button>
                </el-form-item>
            </el-form>
        </div>
        <div class="table">
            <!-- <div class="add">
                <el-button type="primary" @click="add">新增</el-button>
            </div> -->
            <div class="data">
                <el-table :data="tableData" border style="width: 100%">
                    <el-table-column fixed prop="name" label="小区名称" width="150">
                    </el-table-column>
                    <el-table-column prop="place" label="位置">
                    </el-table-column>
                    <el-table-column prop="price" label="价格">
                    </el-table-column>
                    <el-table-column prop="size" label="占地大小">
                    </el-table-column>
                    <el-table-column prop="phone" label="客服电话">
                    </el-table-column>
                    <el-table-column prop="introduce" label="介绍">
                    </el-table-column>
                    <el-table-column fixed="right" label="操作" width="100">
                        <template slot-scope="scope">
                            <el-button type="text" size="small" @click="getRealtyById(scope.row.id)">编辑</el-button>
                            <!-- <el-button @click="deleteRealty(scope.row.id)" type="text" size="small">删除</el-button> -->
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <el-pagination @size-change="handleSizeChange" @current-change="handleCurrentChange" :current-page="pageInfo.page" :page-sizes="[10, 20]" :page-size="pageInfo.size" layout="total, sizes, prev, pager, next, jumper" :total="pageInfo.total">
            </el-pagination>
        </div>
        <el-dialog :title="isAdd?'新增小区信息':'编辑小区信息'" width="600px" :visible.sync="dialogFormVisible" center>
            <el-form :model="addForm" label-width="100px" :rules="addRules" ref="addForm">
                <el-form-item label="小区图片">
                    <el-upload action="http://192.168.101.13:10001/api/images/upload" name="pic" list-type="picture-card" multiple :limit="3" :on-success="uploadImage" :file-list="fileList" :on-remove="handleRemove">
                        <i class="el-icon-plus"></i>
                    </el-upload>
                </el-form-item>
                <el-form-item label="小区名称" prop="name">
                    <el-input v-model="addForm.name"></el-input>
                </el-form-item>
                <el-form-item label="客服电话" prop="phone">
                    <el-input v-model="addForm.phone"></el-input>
                </el-form-item>
                <el-form-item label="介绍" prop="introduce">
                    <el-input v-model="addForm.introduce"></el-input>
                </el-form-item>
                <el-form-item label="位置" prop="place">
                    <el-input v-model="addForm.place"></el-input>
                </el-form-item>
                <el-form-item label="推荐价格" prop="price">
                    <el-input-number :min="0" v-model="addForm.price"></el-input-number>
                </el-form-item>
                <el-form-item label="占地大小" prop="size">
                    <el-input-number :min="0" v-model="addForm.size"></el-input-number>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="dialogFormVisible = false">取 消</el-button>
                <el-button type="primary" @click="commitForm('addForm')">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>

<script>
import api from "../../../api";
export default {
    name: "information",
    data() {
        return {
            formInline: {
                name: "",
                place: "",
            },
            pageInfo: {
                currentPage: 1,
                pageSize: 10,
                total: 0,
            },
            dialogFormVisible: false,
            addForm: {
                name: "",
                phone: "",
                introduce: "",
                place: "",
                price: "",
                size: "",
                images: "",
            },
            fileList: [],
            tableData: [],
            isAdd: true,
            addRules: {
                name: [
                    {
                        required: true,
                        message: "请输入小区名称",
                        trigger: "blur",
                    },
                ],
                introduce: [
                    {
                        required: true,
                        message: "请输入小区介绍",
                        trigger: "blur",
                    },
                ],
                place: [
                    { required: true, message: "请输入位置", trigger: "blur" },
                ],
                size: [
                    {
                        required: true,
                        message: "请输入占地大小",
                        trigger: "blur",
                    },
                ],
                price: [
                    {
                        required: true,
                        message: "请输入推荐价格",
                        trigger: "blur",
                    },
                ],
                phone: [
                    {
                        required: true,
                        message: "请输入手机号",
                        trigger: "blur",
                    },
                    {
                        pattern: /^1[3456789]\d{9}$/,
                        message: "请输入正确手机号",
                        trigger: "blur",
                    },
                ],
            },
        };
    },
    mounted() {
        this.getRealtyList();
    },
    methods: {
        onSubmit() {
            this.pageInfo.currentPage = 1;
            this.getRealtyList();
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
        // 删除图片
        handleRemove(file, fileList) {
            this.fileList = fileList;
        },
        // 上传成功
        uploadImage(res, file, fileList) {
            this.fileList = fileList;
        },
        // 分页获取小区信息数据
        async getRealtyList() {
            try {
                const params = Object.assign(
                    {},
                    this.pageInfo,
                    this.formInline
                );
                const response = await api.realty.getRealtyPage(params);
                if (response.data.code === 200) {
                    this.tableData = response.data.data.records;
                    this.pageInfo.total = response.data.data.total;
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 根据ID获取小区信息
        async getRealtyById(id) {
            this.isAdd = false;
            this.dialogFormVisible = true;
            try {
                const response = await api.realty.getRealtyById({
                    realtyId: id,
                });
                if (response.data.code === 200) {
                    this.addForm = response.data.data;
                    this.fileList = this.addForm.images
                        .split(",")
                        .map((item) => {
                            return {
                                name: item,
                                url: item,
                                response: { data: item },
                            };
                        });
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 添加或者编辑提交
        async commitForm(formName) {
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (this.isAdd) {
                        this.commitAddForm();
                    } else {
                        this.commitEditForm();
                    }
                    this.dialogFormVisible = false;
                    this.addForm = this.$options.data().addForm; //重置表单数据
                }
            });
        },
        // 提交添加用户
        async commitAddForm() {
            try {
                let images = [];
                this.fileList.forEach((element) => {
                    images.push(element.response.data);
                });
                this.addForm.images = images.join(",");
                const response = await api.realty.addRealty(this.addForm);
                if (response.data.code === 200) {
                    this.$message.success("添加成功");
                    this.getRealtyList();
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 提交编辑用户
        async commitEditForm() {
            try {
                let images = [];
                this.fileList.forEach((element) => {
                    images.push(element.response.data);
                });
                this.addForm.images = images.join(",");
                const response = await api.realty.editRealty(this.addForm);
                if (response.data.code === 200) {
                    this.$message.success("编辑成功");
                    this.getRealtyList();
                } else {
                    this.$message.error(response.data.msg);
                }
            } catch (error) {
                this.$message.error(error);
            }
        },
        // 点击新增按钮
        add() {
            this.isAdd = true;
            this.dialogFormVisible = true;
            this.addForm = this.$options.data().addForm;
            this.fileList = [];
        },
        // 删除小区信息
        deleteRealty(id) {
            this.$confirm("确认删除, 是否继续?", "提示", {
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                type: "warning",
            })
                .then(async () => {
                    try {
                        const response = await api.realty.deleteRealty({
                            realtyId: id,
                        });
                        if (response.data.code === 200) {
                            this.$message.success("删除成功");
                            this.getRealtyList();
                        } else {
                            this.$message.error(response.data.msg);
                        }
                    } catch (error) {
                        this.$message.error(error);
                    }
                })
                .catch(() => {});
        },
        handleSizeChange(val) {
            this.pageInfo.size = val;
            this.getRealtyList();
        },
        handleCurrentChange(val) {
            this.pageInfo.currentPage = val;
            this.getRealtyList();
        },
    },
};
</script>

<style lang='less' scoped>
</style>